﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace RTGS.Library.Report
{
   public class ReportSptDetailRecordO
    {

        private string errorLog;
        private string connectionString;

        public ReportSptDetailRecordO(string ConnectionString)
        {
            errorLog = string.Empty;
            connectionString = ConnectionString;
        }

        

        public DataTable GetReportOutgoing(string status)
        {
            DataTable dt = new DataTable();

            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = @"

 SELECT 
ROW_NUMBER()OVER(ORDER BY RECORD_DETAIL_OTR_RelatedTRN DESC) Row
,LTRIM(RTRIM(RECORD_DETAIL_OTR_FromMember)) AS RECORD_DETAIL_OTR_FromMember
,LTRIM(RTRIM(RECORD_DETAIL_OTR_ValueDate)) AS RECORD_DETAIL_OTR_ValueDate
,LTRIM(RTRIM(RECORD_DETAIL_OTR_ToMember)) AS RECORD_DETAIL_OTR_ToMember
,LTRIM(RTRIM(RECORD_DETAIL_OTR_TRN)) AS RECORD_DETAIL_OTR_TRN
,LTRIM(RTRIM(RECORD_DETAIL_OTR_RelatedTRN)) AS RECORD_DETAIL_OTR_RelatedTRN
,LTRIM(RTRIM(RECORD_DETAIL_OST_OriginatingName)) AS RECORD_DETAIL_OST_OriginatingName
,LTRIM(RTRIM(RECORD_DETAIL_OST_UltimateBenefName)) AS RECORD_DETAIL_OST_UltimateBenefName
,LTRIM(RTRIM(RECORD_DETAIL_OST_UltimateBenefAccount)) AS RECORD_DETAIL_OST_UltimateBenefAccount
,ISNULL(LTRIM(RTRIM(RECORD_DETAIL_ORECORD_Status)),'')   +'            '+ ISNULL(LTRIM(RTRIM(RECORD_DETAIL_ORECORD_RTstatus)),'') AS RECORD_DETAIL_ORECORD_Status
,LTRIM(RTRIM(RECORD_DETAIL_ORECORD_RTstatus)) AS RECORD_DETAIL_ORECORD_RTstatus
,LTRIM(RTRIM(RECORD_DETAIL_OTR_Amount)) AS RECORD_DETAIL_OTR_Amount
 FROM STP_RECORD_DETAIL_O
WHERE RECORD_DETAIL_ORECORD_Status='" + status+"'";

                    SqlDataAdapter da = new SqlDataAdapter();
                    da.SelectCommand = cmd;

                    try
                    {
                        conn.Open();
                        da.Fill(dt);
                    }
                    catch (Exception ex)
                    {
                        errorLog = ex.Message;
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }

                }
            }

            return dt;

        }

        public DataTable GetReportSptDetailRecordOPreview(string RelatedTRN)
        {
            DataTable dt = new DataTable();

            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = @"
SELECT RECORD_DETAIL_OTR_FromMember
,RECORD_DETAIL_ORECORD_OH_BatchReference
,RECORD_DETAIL_OTR_ToMember
,RECORD_DETAIL_OST_FromAccountName
,RECORD_DETAIL_OST_ToAccountName
,RECORD_DETAIL_OTR_RelatedTRN	
,RECORD_DETAIL_OTR_Amount
,RECORD_DETAIL_OTR_TRN
,RECORD_DETAIL_OST_OriginatingName
,RECORD_DETAIL_OST_PaymentDetails  
,RECORD_DETAIL_OST_ReceiversReferenceNumber
,RECORD_DETAIL_OST_OriginatingName
,RECORD_DETAIL_OST_UltimateBenefAccount
,RECORD_DETAIL_OST_UltimateBenefName
,RECORD_DETAIL_OST_PaymentDetails
,RECORD_DETAIL_OST_MemberInfo
FROM STP_RECORD_DETAIL_O
WHERE RECORD_DETAIL_OTR_RelatedTRN='" + RelatedTRN.Trim() + "'";

                    SqlDataAdapter da = new SqlDataAdapter();
                    da.SelectCommand = cmd;

                    try
                    {
                        conn.Open();
                        da.Fill(dt);
                    }
                    catch (Exception ex)
                    {
                        errorLog = ex.Message;
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }

                }
            }

            return dt;

        }

        public DataTable GetMember()
        {
            DataTable dt = new DataTable();

            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = @"
SELECT [memcde]
      ,[accnbr]
      ,[memname]
      ,[bankcde]
      ,[brcde1]
      ,[brcde2]
      ,[brcde3]
      ,[brcde4]
      ,[brcde5]
      ,[memtype]
      ,[hqflag]
      ,[offlinebr]
      ,[prmemcde]
      ,[dtstamp]
      ,[memgrp]
  FROM [MEMBERT]
ORDER BY [memname] ASC
";

                    SqlDataAdapter da = new SqlDataAdapter();
                    da.SelectCommand = cmd;

                    try
                    {
                        conn.Open();
                        da.Fill(dt);
                    }
                    catch (Exception ex)
                    {
                        errorLog = ex.Message;
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }

                }
            }

            return dt;

        }

        public DataTable GetMemberByMemberCode(string key)
        {
            DataTable dt = new DataTable();

            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = @"
SELECT  LTRIM(RTRIM([memcde])) AS  [memcde] 
      , LTRIM(RTRIM([accnbr])) AS [accnbr]
      , LTRIM(RTRIM([memname])) AS [memname]

  FROM [MEMBERT]
WHERE [memcde]=@memcde
ORDER BY [memname] ASC
";


                    cmd.Parameters.Add("@memcde", SqlDbType.VarChar).Value = key;

                    SqlDataAdapter da = new SqlDataAdapter();
                    da.SelectCommand = cmd;

                    try
                    {
                        conn.Open();
                        da.Fill(dt);
                    }
                    catch (Exception ex)
                    {
                        errorLog = ex.Message;
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }

                }
            }

            return dt;

        }

    }
}

